package com.lishem.carl._07tree;

import com.lishem.common.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * https://leetcode.cn/problems/binary-tree-right-side-view/description/
 * <p>
 * 给定一个二叉树的 根节点 root，想象自己站在它的右侧，按照从顶部到底部的顺序，返回从右侧所能看到的节点值。
 * <p>
 * 示例 1:
 * <p>
 * 输入: [1,2,3,null,5,null,4]
 * <p>
 * 输出: [1,3,4]
 * <p>
 * 示例 2:
 * <p>
 * 输入: [1,null,3]
 * <p>
 * 输出: [1,3]
 * <p>
 * 示例 3:
 * <p>
 * 输入: []
 * <p>
 * 输出: []
 */
public class _6LetCode199_二叉树的右视图 {

    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                root = queue.poll();
                if (i == size - 1) {
                    res.add(root.val);
                }
                if (root.left != null) {
                    queue.add(root.left);
                }
                if (root.right != null) {
                    queue.add(root.right);
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        _6LetCode199_二叉树的右视图 sol = new _6LetCode199_二叉树的右视图();
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.right = new TreeNode(5);
        root.right = new TreeNode(3);
        root.right.right = new TreeNode(4);
        System.out.println(sol.rightSideView(root));
    }
}
